﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Museum.DBAccess;

public partial class PasswordReset : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                if (ddlOption.SelectedValue.Equals("0"))
                    BindMuseumData();
                else
                    BindSchoolData();

                BindData();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void BindMuseumData()
    {
        ddlMuseum.DataTextField = "MuseumName";
        ddlMuseum.DataValueField = "MuseumId";
        ddlMuseum.DataSource = DBAccess.GetMuseumByCity("");
        ddlMuseum.DataBind();
        BindData();
    }

    private void BindSchoolData()
    {
        ddlMuseum.DataTextField = "SchoolName";
        ddlMuseum.DataValueField = "SchoolId";
        ddlMuseum.DataSource = DBAccess.GetAllSchools();
        ddlMuseum.DataBind();
        BindData();
    }

    private void BindData()
    {
        DataTable dtUserDetails = new DataTable();
        if (!string.IsNullOrEmpty(ddlMuseum.SelectedValue))
            dtUserDetails = DBAccess.GetUserDetails(Convert.ToInt32(ddlMuseum.SelectedValue));
        if (dtUserDetails != null && dtUserDetails.Rows.Count > 0)
        {
            txtUserName.Text = dtUserDetails.Rows[0]["UserName"].ToString();
        }
        else if (!string.IsNullOrEmpty(ddlMuseum.SelectedValue))
            txtUserName.Text = ddlMuseum.SelectedItem.Text;
    }

    private void GeneratePassword()
    {
        if (string.IsNullOrEmpty(txtNewPassword.Text.Trim()) && string.IsNullOrEmpty(txtConfirmNewPassword.Text.Trim()))
            txtNewPassword.Text = Guid.NewGuid().ToString().ToUpper().Substring(0, 10).Replace("-", "");
    }

    protected void ddlMuseum_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int result = 0;
        int role = 0;
        try
        {
            GeneratePassword();

            if (ddlOption.SelectedValue.Equals("0"))
                role = 1;//museum owner
            else
                role = 2;//school owner

            result = DBAccess.InsertOrUpdateUser(Convert.ToInt32(ddlMuseum.SelectedValue), role, txtUserName.Text.Trim(), "", txtNewPassword.Text.Trim());

            if (result.Equals(-1))
            {
                Alert.ShowMessage("User updated successfully.");
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void ddlOption_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlOption.SelectedValue.Equals("0"))
            BindMuseumData();
        else
            BindSchoolData();
    }
}
